package com.cyy.ni.proxy;



import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: Cyy
 * @Description: 代理工厂类ProxyFactory
 * @Date:Created in 18:01 2018/7/19
 */
public class ProxyFactory implements MethodInterceptor {

    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        Enhancer en=new Enhancer();
        en.setSuperclass(target.getClass());
        en.setCallback(this);
        return en.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy proxy) throws Throwable {

        System.out.println("执行目标事务前的操作。。。");
        Object returnValue = method.invoke(target, objects);
        System.out.println("执行目标事务后的操作。。。");

        return returnValue;
    }
}
